package com.tgr.shortLink.admin.controller;

import cn.hutool.core.bean.BeanUtil;
import com.tgr.shortLink.admin.common.convention.result.Result;
import com.tgr.shortLink.admin.common.convention.result.Results;
import com.tgr.shortLink.admin.common.enums.UserEnums;
import com.tgr.shortLink.admin.dto.req.UserLoginReqDTO;
import com.tgr.shortLink.admin.dto.req.UserRegisterRepDTO;
import com.tgr.shortLink.admin.dto.req.UserUpdateRepDTO;
import com.tgr.shortLink.admin.dto.resp.UserActualRespDTO;
import com.tgr.shortLink.admin.dto.resp.UserLoginRespDTO;
import com.tgr.shortLink.admin.dto.resp.UserRespDTO;
import com.tgr.shortLink.admin.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;


/**
 * 描述：
 * 作者：小陶不慌张
 * 文件：UserController
 * 日期：2023/12/15 18:37
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/short-link/admin/v1/")
public class UserController {

    private final UserService userService;

    /**
     * 通过用户名返回用户已脱敏信息
     * @param username 用户名
     * @return 用户脱敏信息
     */
    @GetMapping("/user/{username}")
    public Result<UserRespDTO> getUserByUsername(@PathVariable("username") String username){
        System.out.println("查询到了用户信息");
        return Results.success(userService.getUserByUsername(username));
    }

    /**
     * 通过用户名返回用户真实信息
     * @param username 用户名
     * @return 用户真实信息
     */
    @GetMapping("/actual/user/{username}")
    public Result<UserActualRespDTO> getActualUserByUsername(@PathVariable("username") String username){
        //toBean的作用是将一个JavaBean对象的值复制给另一个JavaBean对象
        return Results.success(BeanUtil.toBean(userService.getUserByUsername(username), UserActualRespDTO.class));
    }

    /**
     * 用户注册
     * @param userRegister 用户注册请求数据
     * @return 空
     */
    @PostMapping("/user")
    public Result<Void> register(@RequestBody UserRegisterRepDTO userRegister){
        userService.register(userRegister);
        return Results.success();
    }

    @GetMapping("/user/has-username")
    public Result<Boolean> hasUsername(@RequestParam("username") String username){
        return Results.success(userService.hasUsername(username));
    }

    @PutMapping("/user")
    public Result<Void> update(@RequestBody UserUpdateRepDTO requestParam){
        userService.update(requestParam);
        return Results.success();
    }

    @PostMapping("/user/login")
    public Result<UserLoginRespDTO> login(@RequestBody UserLoginReqDTO requestParam){
        UserLoginRespDTO userLoginRespDTO = userService.login(requestParam);
        return Results.success(userLoginRespDTO);
    }

    @GetMapping("/user/check-login")
    public Result<Boolean> checkLogin(){
        return Results.success(userService.checkLogin());
    }

    @DeleteMapping("/user/logout")
    public Result<Void> logout(){
        userService.logout();
        return Results.success(null,"用户已退出");
    }

}
